﻿#include "WRWorkDomain.h"
#include "WRWorkSession.h"

namespace WRText
{
    //返回指定工作域状态的文本。
    QString toString(WRWorkDomain::Status value)
    {
        static QString closedString = QStringLiteral("关闭");
        static QString offlineString = QStringLiteral("离线");
        static QString onlineString = QStringLiteral("在线");
        switch(value)
        {
        case WRWorkDomain::Closed:
            return closedString;
        case WRWorkDomain::Offline:
            return offlineString;
        case WRWorkDomain::Online:
            return onlineString;
        default:
            return QString();
        }
    }
}

//初始化工作域对象。
WRWorkDomain::WRWorkDomain(const WRWorkInformation& info, WRWorkDaemon* daemon)
   : WRWorkEntity(info)
{
    m_daemon = daemon;
    m_watchers = new Watchers(this);
}

//清理当前对象所占用的相关资源。
WRWorkDomain::~WRWorkDomain(void)
{
    delete m_watchers;
}

//当加载后执行相应的初始工作。
void WRWorkDomain::onInit(void)
{
    //add code here
}
//当删除前执行相应的清理工作。
void WRWorkDomain::onFree(void)
{
    //add code here
}

//当状态改变后调用此方法。
void WRWorkDomain::Watchers::onChanged(Status status, WRWorkSession* session)
{
    QMutexLocker lock(&m_mutex);
    m_status = status;
    m_session = session;
    for(WRu i = 0; i < m_count; ++i)
    {
        Watcher* watcher = m_array[i];
        watcher->onChanged(m_domain, status, session);
    }
}
